package whf.shop.web;

import java.util.Map;

import whf.file.Constants;
import whf.file.entity.File;
import whf.file.util.DiskFileManager;
import whf.file.util.FileManager;
import whf.framework.entity.AbstractEntity;
import whf.framework.util.Utils;
import whf.framework.util.images.ScaleProperty;
import whf.framework.web.struts.WebData;
import whf.framework.web.struts.WebDispatchAction;
import whf.framework.web.struts.WebForward;
import whf.shop.entity.ProductCategory;
import whf.shop.service.ProductCategoryServiceImp;

/**
 * @author wanghaifeng
 * @email king@126.com
 * @modify 2007-01-11
 */
public class ProductCategoryAction extends WebDispatchAction {

	/*
	 * (non-Javadoc)
	 * 
	 * @see whf.framework.web.struts.WebDispatchAction#prepareCreate(whf.framework.entity.AbstractEntity,
	 *      whf.framework.web.struts.WebData)
	 */
	@Override
	public WebForward prepareCreate(AbstractEntity baseForm, WebData data)
			throws Exception {
		long parentId = data.getLongParameter("ext1", 0);
		if(parentId > 0){
			ProductCategory parent = ProductCategoryServiceImp.getProductCategoryService().findByPrimaryKey(parentId);
			((ProductCategory)baseForm).setParent(parent);
			((ProductCategory)baseForm).setShop(parent.getShop());
		}
		return super.prepareCreate(baseForm, data);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see whf.framework.web.struts.WebDispatchAction#create(whf.framework.entity.AbstractEntity,
	 *      whf.framework.web.struts.WebData)
	 */
	@Override
	public WebForward create(AbstractEntity baseForm, WebData data) throws Exception {
		ProductCategory productCategory = (ProductCategory)baseForm;
		String fileName = productCategory.getImageFileFile().getFileName();
		FileManager fileManager = DiskFileManager.getFileManager();
		if(File.isImageFile(fileName)){
			Map<String, ScaleProperty> props = Utils.newHashMap();
			props.put(Constants.ICON_TYPE_LARGE, Constants.LARGE_SCALE_PROPERTIES);
			props.put(Constants.ICON_TYPE_MIDDLE, Constants.MIDDLE_SCALE_PROPERTIES);
			props.put(Constants.ICON_TYPE_SMALL, Constants.SMALL_SCALE_PROPERTIES);
			fileName = fileManager.saveImage(productCategory.getImageFileFile().getInputStream(), productCategory.getImageFileFile().getFileName(), props);
			productCategory.setImageFile(fileName);
		} else {
			fileName = fileManager.save(productCategory.getImageFileFile().getInputStream(), productCategory.getImageFileFile().getFileName());
			productCategory.setImageFile(fileName);
		}
		return super.create(baseForm, data);
	}

}